﻿using System;

namespace Coding4Fun.Cannon.Phone.Cannon
{
    public static class CannonAim
    {

        //How Fast to pan and tilt
        public static double PanSpeed { get; set; }
        public static double TiltSpeed { get; set; }

        //the current pan and tilt
        public static double CurrentPan { get; private set; }
        public static double CurrentTilt { get; private set; }

        //the value to increment every time we're panning or tilting
        public static double DefaultAngleModifier { get; set; }

        //Min & Max values
        public static double MaxNegativePan { get; set; }
        public static double MaxPositivePan { get; set; }

        public static double MaxTilt { get; set; }
        public static double MinTilt { get; set; }


        static CannonAim()
        {
            PanSpeed = 2;
            TiltSpeed = 1;

            CurrentPan = 0;
            CurrentTilt = 20;

            DefaultAngleModifier = 45;
            
            Logger.WriteLine(String.Format("CannonAim: CurrentPan:{0} CurrentTilt{1}", CurrentPan, CurrentTilt)); 
        }


        public static double CalculatePan(double yAxis)
        {
            var turn = CannonUtility.GetTurn(yAxis);

            switch (turn)
            {
                case TurningAction.Left:
                    CurrentPan = AddCurrentPan();
                    
                    break;
                case TurningAction.Right:
                    CurrentPan = SubtractCurrentPan(); 
                    break;
                case TurningAction.None:
                    //Do nothing
                    break;
                default:
                    break;
            }

            Logger.WriteLine(String.Format("CalculatePan: Action:{0}, CurrentPan{1}", turn, CurrentPan));
            return CurrentPan; 
        }

        public static double CalculateTilt(double zAxis)
        {
            var tilt = CannonUtility.GetTilt(zAxis);
			var inverted = App.ApplicationSettings.IsInverseTilt;

            switch (tilt)
            {
                case TiltingAction.Up:
                    CurrentTilt = (inverted) ? SubtractCurrentTilt() : AddCurrentTilt(); 
                    break;
                case TiltingAction.Down:
					CurrentTilt = (inverted) ? AddCurrentTilt() : SubtractCurrentTilt(); 
                    break;
                default:
                    break;
            }
            Logger.WriteLine(String.Format("CalculateTilt: Action:{0}, CurrentPan{1}", tilt, CurrentTilt));

            return CurrentTilt; 
        }

        private static double AddCurrentTilt()
        {
			if (CurrentTilt >= App.ApplicationSettings.TiltMaximum)
            {
				CurrentTilt = App.ApplicationSettings.TiltMaximum;
            }
            else
            {
                CurrentTilt += TiltSpeed;
            }

            return CurrentTilt; 
        }

        private static double SubtractCurrentTilt()
        {
			if (CurrentTilt <= App.ApplicationSettings.TiltMinimum)
            {
				CurrentTilt = App.ApplicationSettings.TiltMinimum;
            }
            else
            {
                CurrentTilt -= TiltSpeed;
            }

            return CurrentTilt; 
         
        }

        private static double AddCurrentPan()
        {
			if (CurrentPan >= App.ApplicationSettings.PanMaximum)
            {
				CurrentPan = App.ApplicationSettings.PanMaximum;
            }
            else
            {
                CurrentPan += PanSpeed; 
            }

            return CurrentPan; 

        }

        private static double SubtractCurrentPan()
        {
            if (CurrentPan <= App.ApplicationSettings.PanMinimum)
            {
				CurrentPan = App.ApplicationSettings.PanMinimum;
            }
            else
            {
                CurrentPan -= PanSpeed; 
            }

            return CurrentPan; 
        }

        public static double GetAnglePan(double yAxis, double currentPan, double angleModifier)
        {
            // formula is to add current value & angle accelerator            
            // multiply by negative since -90 is left and 90 is right
            return (currentPan + (-yAxis * angleModifier));

        }
    }
}
